

package com.example.demo.demos.web;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serial;
import java.io.Serializable;

public class User implements Serializable {

    @Serial
    private static final long serialVersionUID = -1447349403088719995L;

    private final String name;  // 使用 final 保证不可变（可选）
    private final Integer age;  // 使用 final 保证不可变（可选）
    @JsonCreator
    public User(@JsonProperty("name") String name,
                @JsonProperty("age") Integer age) {
        this.name = name;
        this.age = age;
    }
    // 私有构造方法，只能通过 Builder 创建
    private User(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }

    // Getter 方法
    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    // 静态方法返回 Builder
    public static Builder builder() {
        return new Builder();
    }

    // Builder 内部类
    public static class Builder {
        private String name;
        private Integer age;

        // 私有构造方法
        private Builder() {}

        // 链式调用方法
        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(Integer age) {
            this.age = age;
            return this;
        }

        // 构建 User 对象
        public User build() {
            return new User(this);
        }
    }
}